package com.renhui.videoplayer;

import com.renhui.videoplayer.CustomVideoPlayer.FullScreenListener;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * 视频播放Activity支持小界面和全屏播放
 * 
 * Created by renhui
 */
public class VideoPlayerActivity extends Activity implements FullScreenListener {
	
	private String videoUrl;
	private String imgUrl;
	
	private CustomVideoPlayer videoPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置全屏参数
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_video_player);
		
		videoUrl = getIntent().getStringExtra("video_url");
		imgUrl = getIntent().getStringExtra("img_url");
		videoPlayer = (CustomVideoPlayer) findViewById(R.id.video_player);
		videoPlayer.setUpVideo(videoUrl, imgUrl);
		videoPlayer.setFullScreenListener(this);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// 在设置外层Layer的布局方式之前先调整好VideoView的LayoutParams
		videoPlayer.onConfigurationChanged(newConfig);
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			videoPlayer.setLayoutParams(new LinearLayout.LayoutParams(DeviceScreenUtils.getScreenWidth(), DeviceScreenUtils.getScreenHeight()));
			
			// 全屏 + 隐藏状态栏 和 通知栏
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
			View decor = this.getWindow().getDecorView();
			
			//decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
			decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			
			// PX = DP×（DPI / 160）
			int dpi = getResources().getDisplayMetrics().densityDpi;
			videoPlayer.setLayoutParams(new LinearLayout.LayoutParams(DeviceScreenUtils.getScreenWidth(), (int) (200 * ((float)dpi / (float)160))));
			
			// 退出全屏  + 显示状态栏 和 通知栏
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
			View decor = this.getWindow().getDecorView();
			decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		videoPlayer.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		videoPlayer.onPause();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		videoPlayer.onStop();
	}
	
	@Override
	public void onBackPressed() {
		// 如果当前处于全屏播放模式，就先退出全屏
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			onFullScreenSwitch(false);		
		}  else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			super.onBackPressed();
		}
	}

	@Override
	public void onFullScreenSwitch(boolean toFullScreen) {
		if (toFullScreen) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		} else {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}

	@Override
	public void toFinishActivity() {
		finish();
	}
}
